package com.fs.generator.parser;

import org.apache.commons.lang3.StringUtils;

import com.fs.generator.GenerateConfiguration;

import lombok.Data;

@Data
public class DefaultNamesResolver implements NamesResolver {

	/** 表或列名单词分割符，每个分割符后的单词首字母大写（驼峰规则） */
	private static final String WORD_SEPARATOR = "_";

	private GenerateConfiguration generateConfiguration;

	private String tableToEntityIgnorPrefix;

	private String daoPostfit = "Dao";
	private String daoImplPostfit = "DaoImpl";
	private String daoTestPostfit = "DaoTest";
	private String servicePostfit = "Service";
	private String serviceImplPostfit = "ServiceImpl";
	private String serviceTestPostfit = "ServiceTest";
	private String controllerPostfit = "Controller";

	@Override
	public NamesHold resolve(String tableName) {

		NamesHold namesHold = new NamesHold();
		String baseName = convertBaseName(tableName);

		String rootPackage = getGenerateConfiguration().getRootPackage();
		namesHold.setDomainClassName(StringUtils.capitalize(baseName));
		namesHold.setDomainPackage(rootPackage + ".domain");

		namesHold.setDaoPackage(rootPackage + ".dao");
		namesHold.setDaoClassName(namesHold.getDomainClassName() + daoPostfit);

		namesHold.setDaoImplPackage(rootPackage + ".dao.impl");
		namesHold.setDaoImplClassName(namesHold.getDomainClassName() + daoImplPostfit);

		namesHold.setDaoTestPackage(namesHold.getDaoPackage());
		namesHold.setDaoTestClassName(namesHold.getDomainClassName() + daoTestPostfit);

		namesHold.setServicePackage(rootPackage + ".service");
		namesHold.setServiceClassName(namesHold.getDomainClassName() + servicePostfit);
		namesHold.setServiceImplPackage(rootPackage + ".service.impl");
		namesHold.setServiceImplClassName(namesHold.getDomainClassName() + serviceImplPostfit);

		namesHold.setServiceTestPackage(namesHold.getServicePackage());
		namesHold.setServiceTestClassName(namesHold.getDomainClassName() + serviceTestPostfit);

		namesHold.setControllerPackage(generateConfiguration.getWebPackage());
		namesHold.setControllerClassName(namesHold.getDomainClassName() + controllerPostfit);

		namesHold.setPagePath(getGenerateConfiguration().getWebWorkspace() + "/" + getGenerateConfiguration().getWebappPath() + getGenerateConfiguration().getPagePath());
		return namesHold;

	}

	/**
	 * 表名转换为基础变量名称
	 * 
	 * @param tableName
	 * @return
	 */
	private String convertBaseName(String tableName) {

		String baseName = tableName.toLowerCase();

		if (StringUtils.isNotBlank(tableToEntityIgnorPrefix)) {
			String ignorPrefix = tableToEntityIgnorPrefix.toLowerCase();
			baseName = StringUtils.substringAfter(baseName, ignorPrefix);
		}

		if (baseName.startsWith(WORD_SEPARATOR)) {
			baseName = StringUtils.substringAfter(baseName, WORD_SEPARATOR);
		}
		while (baseName.contains(WORD_SEPARATOR)) {
			baseName = StringUtils.substringBefore(baseName, WORD_SEPARATOR) + StringUtils.capitalize(StringUtils.substringAfter(baseName, WORD_SEPARATOR));
		}
		return baseName;

	}

}
